<?php

class Validator {

    public $_aryError = array();

    function __construct() {
        
    }

    /**
     * validate mail
     * @param <type> $value
     * @param <type> $messageIsNotMail
     * @return <type>
     */
    public function validateMail($value, $messageIsNotMail = null) {

        $isMail = $this->isMail($value);
        if ((!$isMail) && $messageIsNotMail != null) {
            array_push($this->_aryError, $messageIsNotMail);
        }

        return $isMail;
    }

    public function isMail($value) {

        $result = false;
        $pattern = '/^(([a-z0-9!#$%&\/*+-=?^_`\'{|}~]' .
                '[a-z0-9!#$%&\/*+-=?^_`\'{|}~.]*' .
                '[a-z0-9!#$%&\/*+-=?^_`\'{|}~])' .
                '|[a-z0-9!#$%&\/*+-?^_`\'{|}~]|' .
                '("[^",<>]+"))' .
                '[@]((?:[-a-z0-9]+\.)+[a-z]{2,})$/ix';

        $value1 = str_replace('\"', '', $value);
        $result = preg_match($pattern, $value1);
        if ($result) {
            $aryItem = explode('@', $value);
            array_pop($aryItem);
            $value2 = join('@', $aryItem);
            if (strpos($value2, '..') !== false || $value2{0} == '.' || $value2{strlen($value2) - 1} == '.') {
                $result = false;
            }
        }
        return $result;
    }

    public function validateCompare($value1, $value2, $massegeErrorCompare = null) {
        $resutl = false;
        if (trim($value1) == trim($value2)) {
            $resutl = true;
        }
        if ($resutl == false && isset($massegeErrorCompare)) {
            array_push($this->_aryError, $massegeErrorCompare);
        }
        return $resutl;
    }

    public function validateEmpty($var, $messageIsEmpty = null) {
        $result = false;
        if (trim(strip_tags($var)) == "") {
            $result = true;
        }

        if (($result == true) && $messageIsEmpty != null) {
            array_push($this->_aryError, $messageIsEmpty);
        }

        return $result;
    }

    public function validateDateFormat($date, $messageIsNotDateFormat = null) {
        $isOK = $this->isValidDateFormat($date);
        if ((!$isOK) && $messageIsNotDateFormat != null) {
            array_push($this->_aryError, $messageIsNotDateFormat);
        }

        return $isOK;
    }

    public function isValidDateFormat($date) {
        if ($date === null)
            return true;
        // match the format of the date
        if (preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $date, $parts)) {
            // check weather the date is valid of not
//            echo $parts[3];
            if (checkdate($parts[2], $parts[1], $parts[3]))
                return true;
            else
                return false;
        }
        else
            return false;
    }

    public function validateNumber($value, $type='int', $messageIsNotNumber=null) {

        $isOK = $this->isNumber($value, $type);
        if ((!$isOK) && $messageIsNotNumber != null) {
            array_push($this->_aryError, $messageIsNotNumber);
        }

        return $isOK;
    }

    public function isNumber($value, $type = 'int') {
        return $this->checkNumber($value, $type);
    }

    /**
     * Check number
     *
     * @param mixed $value
     * @param string $type (int | integer | decimal | float)
     */
    public function checkNumber($value, $type = 'int') {
        $res = true;
        $value = trim($value);
        if (is_numeric($value)) {
            if (strtolower($type) == 'int' || strtolower($type) == 'integer') {
                if ((float) $value != (int) $value) {
                    $res = false;
                }
            } elseif (strtolower($type) == 'decimal' || strtolower($type) == 'float') {
                if (substr_count($value, '.') != 1) {
                    $res = false;
                }
            }
        } else {
            $res = false;
        }
        return $res;
    }

    /**
     * Check phone
     *
     * @param mixed $value
     */
    public function validatePhone($value, $messagePhoneError = null) {
        $value = str_replace(' ', '', $value);
        $value = str_replace('-', '', $value);
        $value = str_replace('.', '', $value);
        $intOk = $this->isPositiveInt($value);
        if ($intOk == false && $messagePhoneError != null) {
            array_push($this->_aryError, $messagePhoneError);
        }
        return $intOk;
    }

    /**
     * Check if a number is positive integer
     *
     * @author  ThuanVN
     * @param string $string
     * @return boolean
     */
    public function isPositiveInt($number) {
        return (preg_match('/^([0-9]+)$/i', $number) && $number);
    }

    /**
     * getResullAndError
     *
     * @author ThanhDN 2010/05/20.
     * @return <type>
     */
    public function getResultAndErrors(&$aryError) {
        $aryError = array();
        if ($this->isValidate()) {
            return 1;
        } else {
            $aryTemp = $this->getErrors();
            $aryError = array_merge($aryError, $aryTemp);
            return -2;
        }
    }

    /**
     * hasError
     *
     * @author ThanhDN 2010/05/20.
     * @return <type>
     */
    public function isValidate() {
        return (isset($this->_aryError[0]) ? false : true);
    }

    /**
     * getError
     *
     * @author ThanhDN 2010/05/20.
     * @return <type>
     */
    public function getErrors() {
        return $this->_aryError;
    }

}

?>
